package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.product.domain.FaultType;
import com.ruoyi.product.domain.FaultType;
import com.ruoyi.product.domain.WorkOrder;
import com.ruoyi.product.domain.dto.FaultTypeDto;
import com.ruoyi.product.domain.vo.FaultTypeVo;
import com.ruoyi.product.domain.vo.FaultTypeVo;
import com.ruoyi.product.mapper.FaultTypeMapper;
import com.ruoyi.product.service.FaultTypeService;
import com.ruoyi.product.mapper.FaultTypeMapper;
import com.ruoyi.quality.domain.InspectionTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Service
public class FaultTypeServiceImpl implements FaultTypeService{

    @Resource
    FaultTypeMapper faultTypeMapper;

    @Override
    public Page<FaultTypeVo> getFaultTypeByPage(FaultTypeDto pageEntity) {
        MPJLambdaWrapper<FaultType> wrapper = new MPJLambdaWrapper<FaultType>()
                .selectAll(FaultType.class)//查询user表全部字段
                //记的测试阶段
                .eq(FaultType::getDelFlag, 0)
                .orderByDesc(FaultType::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, FaultType.class, pageEntity);
        Page<FaultTypeVo> userList = faultTypeMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   FaultTypeVo.class, wrapper);
        return userList;
    }

    @Override
    public void addFaultType(FaultTypeDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<FaultType> wrapper = new MPJLambdaWrapper<FaultType>()
                .eq(FaultType::getCode, testDto.getCode())
                .ne(FaultType::getDelFlag, 1);
        FaultType material = faultTypeMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        FaultType test2 = new FaultType();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        faultTypeMapper.insert(test2);
    }

    @Override
    public void updateFaultType(FaultTypeDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<FaultType> wrapper = new MPJLambdaWrapper<FaultType>()
                .eq(FaultType::getCode, testDto.getCode());
        FaultType faultType = faultTypeMapper.selectOne(wrapper);
        if(null == faultType) {
            throw new GlobalException("该FaultType不存在");
        }
        BeanUtils.copyProperties(testDto, faultType);
        faultType.setUpdateBy(userName);
        faultType.setUpdateTime(new Date());
        faultTypeMapper.updateById(faultType);
    }

    @Override
    public void delFaultType(FaultTypeDto testDto) {
        FaultType material = faultTypeMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该FaultType不存在");
        }
        material.setDelFlag(true);
        faultTypeMapper.updateById(material);
    }
}




